package com.mdxx.xczd.ui.presenter;

import android.text.TextUtils;
import com.google.gson.Gson;
import com.mdxx.xczd.app.App;
import com.mdxx.xczd.bean.Alarm;
import com.mdxx.xczd.bean.AlarmBean;
import com.mdxx.xczd.bean.AlarmBeanResult;
import com.mdxx.xczd.bean.CancelAlarmResult;
import com.mdxx.xczd.bean.FileBean;
import com.mdxx.xczd.bean.FinishedPoint;
import com.mdxx.xczd.bean.SubmitFileResult;
import com.mdxx.xczd.bean.TaskBean;
import com.mdxx.xczd.bean.TaskBeanResult;
import com.mdxx.xczd.bean.VideoBean;
import com.mdxx.xczd.ui.contract.TaskContract;
import com.mdxx.xczd.utils.FormatUtil;
import com.mdxx.xczd.utils.PictureUtil;
import com.mdxx.xczd.utils.ToastUtil;
import com.mdxx.xczd.utils.VideoUtil;
import com.orhanobut.logger.Logger;
import java.io.File;
import java.net.ConnectException;
import java.net.SocketTimeoutException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeoutException;

import rx.Observer;

/**
 * Created by MD-02 on 2017/1/17.
 */

public class TaskPresenter extends TaskContract.Presenter {

    @Override
    public void getTaskList() {
        mModel.getTaskList()
                .subscribe(new Observer<TaskBeanResult>() {
                    @Override
                    public void onCompleted() {
                    }

                    @Override
                    public void onError(Throwable e) {
                /*Logger.d("e="+e.toString());*/
                        ToastUtil.showShort(e.getMessage());
                    }

                    @Override
                    public void onNext(TaskBeanResult taskBeanResult) {
                  /*  Logger.json( new Gson().toJson(taskBeanResult));*/
                        List<TaskBean> list = new ArrayList<TaskBean>();
                        List<TaskBean> taskList = new ArrayList<TaskBean>();
                        List<TaskBean> orderList = new ArrayList<TaskBean>();
                        if (taskBeanResult.code == 0) {
                            list = taskBeanResult.data;
                            for (int i = 0; i < list.size(); i++) {
                                if (list.get(i).state == 10 || list.get(i).state == 11) {
                                    taskList.add(list.get(i));
                                } else if (list.get(i).state == 0) {
                                    orderList.add(list.get(i));
                                }
                            }
                            if (taskList.size() > 0) {
                                App.taskBean = taskList.get(0);
                            }
                        }
                        mView.returnTaskList(taskList, orderList);
                    }
                });
    }

    @Override
    public void submitFile(String path, String type) {
        String strTaskNum = App.taskBean == null ? " ": App.taskBean.taskNum;
        FileBean fileBean = new FileBean();
        if (TextUtils.equals("photo", type)) {
            File smallFile = PictureUtil.toSmallImg(path);
            fileBean.setPath(smallFile.getAbsolutePath());
        } else if (TextUtils.equals("video", type)) {
            String[] paths = VideoUtil.getBitmapsFromVideo(path);
            fileBean.setPaths(paths);
        }
        fileBean.setEmpNum(App.userBean.empNum);
        fileBean.setLa(String.valueOf(App.mPoint.la));
        fileBean.setLo(String.valueOf(App.mPoint.lo));
        if (App.taskBean != null) {
            fileBean.setTaskNum(App.taskBean.taskNum);
        } else {
            fileBean.setTaskNum(strTaskNum);
        }
        Logger.d("App.alarmNum="+App.alarmNum);
        if (App.alarmNum != null) {
            fileBean.setAlarmNum(App.alarmNum);
        }
        fileBean.setType(type);
        fileBean.setUploadTime(FormatUtil.getPointDate());
        mModel.submitFile(fileBean)
                .subscribe(new Observer<SubmitFileResult>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        ToastUtil.showShort(e.getMessage());
                    }

                    @Override
                    public void onNext(SubmitFileResult submitFileResult) {
                        if (submitFileResult.getCode() == 0) {
                            ToastUtil.showShort(submitFileResult.getSuccessMsg());
                        } else {
                            ToastUtil.showShort(submitFileResult.getErrorMsg());
                        }
                    }
                });
    }

    @Override
    public void submitVideo(final VideoBean videobean) {
        mModel.submitVideo(videobean)
                .subscribe(new Observer<SubmitFileResult>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        ToastUtil.showShort(e.getMessage());
                    }

                    @Override
                    public void onNext(SubmitFileResult submitFileResult) {
                        if (submitFileResult.getCode() == 0) {
                            mView.returnVideo(videobean);
                            ToastUtil.showShort(submitFileResult.getSuccessMsg());
                        } else {
                            ToastUtil.showShort(submitFileResult.getErrorMsg());
                        }
                    }
                });
    }

    @Override
    public void sendAlarm(final String type) {
        String strTaskNum = App.taskBean == null ? " " : App.taskBean.taskNum;
        Alarm alarm = new Alarm();
        alarm.setAlarmLevel(type);
        alarm.setAlarmLola(App.mPoint);
        alarm.setTaskNum(strTaskNum);
        alarm.setAddrStr(App.addrstr);
        alarm.setEmpNum(App.USERID);
        mModel.sendAlarm(alarm)
                .subscribe(new Observer<AlarmBeanResult>() {
                    @Override
                    public void onCompleted() {
                    }

                    @Override
                    public void onError(Throwable e) {
                        Logger.d("sendAlarm----"+e.toString());
                        if (e instanceof TimeoutException || e instanceof SocketTimeoutException
                                || e instanceof ConnectException){
                            mView.returnAlarm("-1");
                        }
                        ToastUtil.showShort(e.getMessage());
                    }

                    @Override
                    public void onNext(AlarmBeanResult alarmBeanResult) {
                        Logger.d("sendAlarm----"+new Gson().toJson(alarmBeanResult).toString());
                        if (alarmBeanResult.getCode() == 0) {
                            AlarmBean alarmBean = alarmBeanResult.getData();
                            String alarmNum = alarmBean.getAlarmNum();
                            App.alarmNum = alarmNum;
                            ToastUtil.showShort(alarmBeanResult.getSuccessMsg());
                            mView.returnAlarm(type);
                        } else {
                            ToastUtil.showShort(alarmBeanResult.getErrorMsg());
                        }
                    }
                });
    }

    @Override
    public void submitLocation(String coordinate) {
        mModel.submitLocation(coordinate)
                .subscribe(new Observer<TaskBeanResult>() {
                    @Override
                    public void onCompleted() {
                    }

                    @Override
                    public void onError(Throwable e) {
                        ToastUtil.showShort(e.getMessage());
                    }

                    @Override
                    public void onNext(TaskBeanResult TaskBeanResult) {

                    }
                });
    }

    @Override
    public void submitFinishedPoint(FinishedPoint finishedPoint) {
        mModel.submitFinishedPoint(finishedPoint)
                .subscribe(new Observer<TaskBeanResult>() {
                    @Override
                    public void onCompleted() {
                    }

                    @Override
                    public void onError(Throwable e) {
                        ToastUtil.showShort(e.getMessage());
                    }

                    @Override
                    public void onNext(TaskBeanResult TaskBeanResult) {

                    }
                });
    }

    @Override
    public void cancelAlarm(String cancelAlarm) {
        mModel.cancelAlarm(cancelAlarm)
                .subscribe(new Observer<CancelAlarmResult>() {
                    @Override
                    public void onCompleted() {
                    }

                    @Override
                    public void onError(Throwable e) {
                        ToastUtil.showShort(e.getMessage());
                    }

                    @Override
                    public void onNext(CancelAlarmResult cancelAlarmResult) {

                    }
                });
    }

}
